package com.yutian.endecryption.mock.component;

import com.yutian.endecryption.mock.service.MockDataService;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import static com.yutian.endecryption.config.Config.CAR_TYPE;

/**
 * 向数据库中添加模拟正式考试考生信息的工具
 *
 * @author zhang peng yang ming
 * @create 2017-07-31 10:14
 */
public class JTabbedMock extends JPanel implements MouseListener{
    private JButton add, clear, reset, register;
    private JTextField idNum, name;
    private JComboBox carType;

    public JTabbedMock () {
        setLayout(new BorderLayout());

        JPanel wrapContent = new JPanel();
        JPanel wrapFooter = new JPanel();
        JPanel idNumWrap = new JPanel();
        JPanel nameWrap = new JPanel();
        JPanel carTypeWrap = new JPanel();

        SpringLayout lay = new SpringLayout();
        wrapContent.setLayout(lay);

        idNumWrap.setBorder(BorderFactory.createTitledBorder("身份证明号码"));
        nameWrap.setBorder(BorderFactory.createTitledBorder("姓名"));
        carTypeWrap.setBorder(BorderFactory.createTitledBorder("考试车型"));
        idNumWrap.setLayout(new BorderLayout());
        nameWrap.setLayout(new BorderLayout());
        carTypeWrap.setLayout(new BorderLayout());

        idNum = new JTextField();
        name = new JTextField();
        carType = new JComboBox(CAR_TYPE);

        idNumWrap.add(idNum);
        nameWrap.add(name);
        carTypeWrap.add(carType);

        wrapContent.add(idNumWrap);
        wrapContent.add(nameWrap);
        wrapContent.add(carTypeWrap);

        //////////////////  添加按钮  //////////////////

        add = new JButton(" 添加 ");
        reset = new JButton(" 重置 ");
        register = new JButton("一键签到");
        clear = new JButton(" 清除数据 ");

        wrapFooter.add(add);
        wrapFooter.add(reset);
        wrapFooter.add(register);
        wrapFooter.add(clear);

        lay.putConstraint(SpringLayout.NORTH, idNumWrap, 0, SpringLayout.NORTH, wrapContent);
        lay.putConstraint(SpringLayout.EAST, idNumWrap, 0, SpringLayout.EAST, wrapContent);
        lay.putConstraint(SpringLayout.WEST, idNumWrap, 0, SpringLayout.WEST, wrapContent);

        lay.putConstraint(SpringLayout.NORTH, nameWrap, 45, SpringLayout.NORTH, wrapContent);
        lay.putConstraint(SpringLayout.EAST, nameWrap, 0, SpringLayout.EAST, wrapContent);
        lay.putConstraint(SpringLayout.WEST, nameWrap, 0, SpringLayout.WEST, wrapContent);

        lay.putConstraint(SpringLayout.NORTH, carTypeWrap, 90, SpringLayout.NORTH, wrapContent);
        lay.putConstraint(SpringLayout.EAST, carTypeWrap, 0, SpringLayout.EAST, wrapContent);
        lay.putConstraint(SpringLayout.WEST, carTypeWrap, 0, SpringLayout.WEST, wrapContent);

        add(BorderLayout.CENTER, wrapContent);
        add(BorderLayout.SOUTH, wrapFooter);

        add.addMouseListener(this);
        clear.addMouseListener(this);
        register.addMouseListener(this);
        reset.addMouseListener(this);
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        if (e.getSource().equals(add)) {
            int result = MockDataService.generateMockData (
                    idNum.getText().trim(), name.getText().trim(), carType.getSelectedIndex());

            if (result != 0) {
                JOptionPane.showMessageDialog(null, "添加成功");
            }
        } else if (e.getSource().equals(clear)) {
            int r = JOptionPane.showOptionDialog(null, "确认要清除所有模拟考试数据？", "确认", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE, null, null, null);

            if (r == 0) {
                r = MockDataService.clearMockData();
                JOptionPane.showMessageDialog(null, "清除成功");
            }
        } else if (e.getSource().equals(register)) {
            int r = 1;
            if (r == 0) {
                JOptionPane.showMessageDialog(null, "该功能未实现,敬请期待下一版本");
            } else {
                r =MockDataService.signAll();
                JOptionPane.showMessageDialog(null, "总共 【" + r + "】 个学员签到完成");
            }
        } else if (e.getSource().equals(reset)) {
            idNum.setText("");
            name.setText("");
            carType.setSelectedIndex(0);
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }
}
